const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    merchandise_array: [],
    showModal: false,
    inputValue: '',
    showEditModal: false,
    inputEditValue: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let _that = this;
    app.getToken().then(function (){
      _that.loadData();
    });
  },

  loadData: function (){
    const that = this;
    wx.showLoading({
      title: '加载中',
    });
    wx.request({
      url: app.globalData.requestUrl + 'api/category/lists',
      method: 'POST',
      data: {
        user_token: app.globalData.userToken,
      },
      success: function (res) {
        wx.hideLoading()
        // 执行成功
        that.setData({
          merchandise_array: res.data.data.lists
        })

      }, fail: function () {

        wx.showToast({
          title: '系统错误请稍后重试',
          icon: 'none'
        });
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //创建品类
  /**
     * 弹窗
     */
  showDialogBtn: function () {
    this.setData({
      showModal: true
    })
  },
  showEditDialogBtn: function (e) {
    var tid = e.currentTarget.dataset.tid;
    var tname = e.currentTarget.dataset.tname;
    this.setData({
      showEditModal: true,
      categoryId: tid,
      categoryNameNew: tname
    })
  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () {
  },
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  hideEditModal: function () {
    this.setData({
      showEditModal: false
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
  },
  onEditCancel: function () {
    this.hideEditModal();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function (e) {
    const that = this;
    let category_name = that.data.inputValue;
    if (category_name == '') {
      wx.showToast({
        title: '请输入品类名',
        icon: 'none',
      });
      return;
    }
    that.hideModal();
    wx.showToast({
      title: '正在提交信息...',
      icon: 'none',
      duration: 200000
    });

    wx.request({
      url: app.globalData.requestUrl + 'api/category/create',
      method: 'POST',
      data: {
        user_token: app.globalData.userToken,
        category_name: category_name,
      },
      success: function (res) {
        wx.hideToast();
        // 执行成功
        that.setData({
          merchandise_array: res.data.data.categories
        })

      }, fail: function () {

        wx.showToast({
          title: '系统错误请稍后重试',
          icon: 'none'
        });
      }
    });

  },
  onEditConfirm: function (e) {
    const that = this;
    let category_name = that.data.inputEditValue;
    if (category_name == '') {
      wx.showToast({
        title: '请输入品类名',
        icon: 'none',
      });
      return;
    }
    that.hideModal();
    wx.showToast({
      title: '正在提交信息...',
      icon: 'none',
      duration: 200000
    });

    wx.request({
      url: app.globalData.requestUrl + 'api/category/edit',
      method: 'POST',
      data: {
        user_token: app.globalData.userToken,
        id: that.data.categoryId,
        category_name: category_name,
      },
      success: function (res) {
        wx.hideToast();
        // 执行成功
        that.setData({
          merchandise_array: res.data.data.categories
        })

        that.onLoad();

      }, fail: function () {

        wx.showToast({
          title: '系统错误请稍后重试',
          icon: 'none'
        });
      }
    });

    this.hideEditModal();

  },
  inputChange: function (e) {
    const that = this;
    that.setData({
      inputValue: e.detail.value
    })
  },
  inputEditChange: function (e) {
    const that = this;
    that.setData({
      inputEditValue: e.detail.value
    })
  },
  //删除品类
  delMerchandise: function (e) {
    const that = this;
    let id = e.currentTarget.id;
    wx.showModal({
      content: '是否删除此品类',
      success: function (res) {
        if (res.confirm) {
          wx.showToast({
            title: '正在提交信息...',
            icon: 'none',
            duration: 200000
          });
          wx.request({
            url: app.globalData.requestUrl + 'api/category/delete',
            method: 'POST',
            data: {
              user_token: app.globalData.userToken,
              id: id,
            },
            success: function (res) {
              wx.hideToast();
              // 执行成功
              if(res.data.status == 1) {
                that.setData({
                  merchandise_array: res.data.data.categories
                })
              }else{

                wx.showToast({
                  title: ret.data.msg,
                  icon: 'none',
                });
              }

            }, fail: function () {

              wx.showToast({
                title: '系统错误请稍后重试',
                icon: 'none'
              });
            }
          });

        } else if (res.cancel) {
          wx.showToast({
            title: '删除取消',
            icon: 'none',
          });
        }
      }
    })

  }
})